##################################################
# Replication Code
# Taeyong Park and Andrew Reeves
# "Local Unemployment and Voting for President: Uncovering Causal Mechanisms"
# Summary: Recoding and Data Analysis for 2008 -- Treatment: Median Household Income
##################################################

rm(list = ls())
library(foreign)
library(stringr)
library(R2WinBUGS);library(rjags);library(R2jags);library(coda);library(superdiag); library(MCMCpack)
library(abind)

data = read.dta("data08Imputed1.dta") 

# Evaluations
data$VBad = rep(0, nrow(data))
data$VBad[data$natecon5==5] = 1
data$Bad = rep(0, nrow(data))
data$Bad[data$natecon5==4] = 1
data$Good = rep(0, nrow(data))
data$Good[data$natecon5==2] = 1
data$VGood = rep(0, nrow(data))
data$VGood[data$natecon5==1] = 1

# Age
data$younger30 = rep(0, nrow(data))
data$younger30[data$age<30] = 1
data$older65 = rep(0, nrow(data))
data$older65[data$age>=65] = 1

# Race
data$black = rep(0, nrow(data))
data$black[data$race==1]=1
data$latino = rep(0, nrow(data))
data$latino[data$race==2]=1

# Employment status
data$fullTime = rep(0, nrow(data))
data$fullTime[data$employment == 1] = 1 
data$partTime = rep(0, nrow(data))
data$partTime[data$employment == 2] = 1 
data$unemployed = rep(0, nrow(data))
data$unemployed[data$employment == 3] = 1 

# Education
data$noHighSchool = rep(0, nrow(data))
data$noHighSchool[data$educ==4] = 1
data$HighSchool = rep(0, nrow(data))
data$HighSchool[data$educ==3] = 1
data$someCollege = rep(0, nrow(data))
data$someCollege[data$educ==1] = 1
data$fourCollege = rep(0, nrow(data))
data$fourCollege[data$educ==2] = 1

# Party ID
data$Rep = rep(0, nrow(data))
data$Rep[data$party3==3] = 1
data$Ind = rep(0, nrow(data))
data$Ind[data$party3==2] = 1
data$Dem = rep(0, nrow(data))
data$Dem[data$party3==1] = 1

# News interest
data$Very=ifelse(data$newsInt==4, 1, 0)
data$Some=ifelse(data$newsInt==3, 1 ,0)
data$Few=ifelse(data$newsInt==1 | data$newsInt==2, 1, 0)

# Unemployment
data$avgUnemp07 = apply(data[,7:18], 
                        FUN=mean, MARGIN=1)
data$avgUnemp08 = apply(data[,19:30], 
                        FUN=mean, MARGIN=1)
data$chgUnemp0807=data$avgUnemp08-data$avgUnemp07

# Income 
data$chgInc0807=(data$inc08-data$inc07)/1000
data$inc08=data$inc08/1000

# Gas price
data$avgGas = (data$gasOct08 + data$gasSept08 + data$gasAug08)/3

# Foreclosure
data$totForcAugOct = data$forcAug08  + data$forcSep08+   data$forcOct08



########################################
#
# MEDIATION ANALYSIS STEP2
# A part of the code below is from Becher and Donnelly (2013)
# 
########################################

# Step 1 for Income is the same as Step 1 for Unemployment.
# Below code uses the data produced by dataAnalysis08_Unemp.R to start with Step 2.

attach(data)

######## step2Model1 #######

withmat_mcmc = read.table("mcmc08_step1Model1")
colnames(withmat_mcmc[801:1000,1:25])

mcmcID=NA
for(i in 1:25){ # This is to adjust the order of the colnames of mcmc; the order is NOT 1,2,3,... BUT 1,10,100,101, ...
  ID=str_extract(colnames(withmat_mcmc)[1:25][i], "[0-9]+$") # Extract the digits located at the end of each string 
  mcmcID[i]=as.numeric(ID) 
}
colnames(withmat_mcmc[801:1000,order(mcmcID)]) # Make sure to reorder the columns

newcoeftotal = withmat_mcmc[801:1000,order(mcmcID)]

cut1 = apply(withmat_mcmc[,77:280][,which(str_detect(colnames(withmat_mcmc)[77:280], "1.$"))], 1 , FUN=mean)
cut2 = apply(withmat_mcmc[,77:280][,which(str_detect(colnames(withmat_mcmc)[77:280], "2.$"))], 1 , FUN=mean)
cut3 = apply(withmat_mcmc[,77:280][,which(str_detect(colnames(withmat_mcmc)[77:280], "3.$"))], 1 , FUN=mean)
cut4 = apply(withmat_mcmc[,77:280][,which(str_detect(colnames(withmat_mcmc)[77:280], "4.$"))], 1 , FUN=mean)

newcoeftotal = as.matrix(cbind(newcoeftotal, cut1[801:1000], cut2[801:1000], cut3[801:1000], cut4[801:1000]))


sims = 200 # 200 sets of simulation 

# Simulate potential values that will be used to generate predictions.

Xm25 =cbind(chgUnemp0807, avgUnemp08,  rep(-2.5, nrow(data)), inc08, avgGas, totForcAugOct,  
             younger30, older65, female, black, latino, noHighSchool, HighSchool, someCollege, fourCollege,  
             income, unemployed, fullTime, partTime, ownHome,
             newsInt, ideol, Dem, Rep, Ind)
Xm20 =cbind(chgUnemp0807, avgUnemp08,  rep(-2.0, nrow(data)), inc08, avgGas, totForcAugOct,  
             younger30, older65, female, black, latino, noHighSchool, HighSchool, someCollege, fourCollege,  
             income, unemployed, fullTime, partTime, ownHome,
             newsInt, ideol, Dem, Rep, Ind)
Xm15 =cbind(chgUnemp0807, avgUnemp08,  rep(-1.5, nrow(data)), inc08, avgGas, totForcAugOct,  
             younger30, older65, female, black, latino, noHighSchool, HighSchool, someCollege, fourCollege,  
             income, unemployed, fullTime, partTime, ownHome,
             newsInt, ideol, Dem, Rep, Ind)
Xm10 =cbind(chgUnemp0807, avgUnemp08,  rep(-1.0, nrow(data)), inc08, avgGas, totForcAugOct,  
             younger30, older65, female, black, latino, noHighSchool, HighSchool, someCollege, fourCollege,  
             income, unemployed, fullTime, partTime, ownHome,
             newsInt, ideol, Dem, Rep, Ind)
Xm5 =cbind(chgUnemp0807, avgUnemp08,  rep(-.5, nrow(data)), inc08, avgGas, totForcAugOct,  
            younger30, older65, female, black, latino, noHighSchool, HighSchool, someCollege, fourCollege,  
            income, unemployed, fullTime, partTime, ownHome,
            newsInt, ideol, Dem, Rep, Ind)
X0 =cbind(chgUnemp0807, avgUnemp08,  rep(0, nrow(data)), inc08, avgGas, totForcAugOct,  
           younger30, older65, female, black, latino, noHighSchool, HighSchool, someCollege, fourCollege,  
           income, unemployed, fullTime, partTime, ownHome,
           newsInt, ideol, Dem, Rep, Ind)
X5 =cbind(chgUnemp0807, avgUnemp08,  rep(.5, nrow(data)), inc08, avgGas, totForcAugOct,  
           younger30, older65, female, black, latino, noHighSchool, HighSchool, someCollege, fourCollege,  
           income, unemployed, fullTime, partTime, ownHome,
           newsInt, ideol, Dem, Rep, Ind)
X10 =cbind(chgUnemp0807, avgUnemp08,  rep(1.0, nrow(data)), inc08, avgGas, totForcAugOct,  
            younger30, older65, female, black, latino, noHighSchool, HighSchool, someCollege, fourCollege,  
            income, unemployed, fullTime, partTime, ownHome,
            newsInt, ideol, Dem, Rep, Ind)
X15 =cbind(chgUnemp0807, avgUnemp08,  rep(1.5, nrow(data)), inc08, avgGas, totForcAugOct,  
            younger30, older65, female, black, latino, noHighSchool, HighSchool, someCollege, fourCollege,  
            income, unemployed, fullTime, partTime, ownHome,
            newsInt, ideol, Dem, Rep, Ind)
X20 =cbind(chgUnemp0807, avgUnemp08,  rep(2.0, nrow(data)), inc08, avgGas, totForcAugOct,  
            younger30, older65, female, black, latino, noHighSchool, HighSchool, someCollege, fourCollege,  
            income, unemployed, fullTime, partTime, ownHome,
            newsInt, ideol, Dem, Rep, Ind)
X25 =cbind(chgUnemp0807, avgUnemp08,  rep(2.5, nrow(data)), inc08, avgGas, totForcAugOct,  
            younger30, older65, female, black, latino, noHighSchool, HighSchool, someCollege, fourCollege,  
            income, unemployed, fullTime, partTime, ownHome,
            newsInt, ideol, Dem, Rep, Ind)

n_obs=nrow(data)

# Use this function to create a matrix of predicted national economic evaluations
assignval=function(c1, c2, c3, c4, val){
  if (val<c1){out= 1}
  else if (val<c2){out = 2}
  else if (val<c3){out = 3}
  else if (val<c4){out = 4}
  else {out= 5}
  out
}

bigmat = matrix(nrow = n_obs, ncol = sims*11) # 11 comes from the number of potential value sets


for (i in 1:sims){
  newcoef = newcoeftotal[i,]
  errs = rnorm(n_obs, mean = 0, sd = 1)
  bigmat[ , (i-1)*11+1] = mapply(rep(newcoef[26], n_obs), 
                                  FUN = assignval, 
                                  c2 = rep(newcoef[27], n_obs), 
                                  c3 = rep(newcoef[28], n_obs),
                                  c4 = rep(newcoef[29], n_obs),
                                  val = Xm25%*%as.matrix(newcoef[1:25]) + errs) 
  # val produces predicted latent values of economic evaluation for every observation: Xm25 is multiplied by the estimated coefficients.
  # And then, function "assignval" compares the magnitude of the predicted latent value for an observation
  # with the four estimated cutpoints that are cut1 from newcoef[26], cut2 from newcoef[27], and so forth.
  # Finally, if the predicted latent value for an observation is smaller than cut1, the "assignval" function assigns 1,
  # else if it is smaller than cut2, the function assigns 2, and so on and on.
  # Consequently, the first column of bigmat includes the predicted economic evaluations (1,2,3,4, or 5) associated with Xm25 and the first estimated coefficient sample out of 200 (the number of sims) that comes from newcoef[1,].
  # And, the twelfth column of bigmat includes the predicted economic evaluations (1,2,3,4, or 5) associated with Xm25 and the second estimated coefficient sample out of 200 (the number of sims).
  # In the same vein, the second column of bigmat is associated with Xm20 and the first estimated coefficient sample.
  # And, the thirteenth column of bigmat is associated with Xm20 and the second estimated coefficient sample.
  bigmat[ , (i-1)*11+2] = mapply(rep(newcoef[26], n_obs), 
                                  FUN = assignval, 
                                  c2 = rep(newcoef[27], n_obs), 
                                  c3 = rep(newcoef[28], n_obs),
                                  c4 = rep(newcoef[29], n_obs),
                                  val = Xm20%*%as.matrix(newcoef[1:25]) + errs)  
  bigmat[ , (i-1)*11+3] = mapply(rep(newcoef[26], n_obs), 
                                  FUN = assignval, 
                                  c2 = rep(newcoef[27], n_obs), 
                                  c3 = rep(newcoef[28], n_obs),
                                  c4 = rep(newcoef[29], n_obs),
                                  val = Xm15%*%as.matrix(newcoef[1:25]) + errs) 
  bigmat[ , (i-1)*11+4] = mapply(rep(newcoef[26], n_obs), 
                                  FUN = assignval, 
                                  c2 = rep(newcoef[27], n_obs), 
                                  c3 = rep(newcoef[28], n_obs),
                                  c4 = rep(newcoef[29], n_obs),
                                  val = Xm10%*%as.matrix(newcoef[1:25]) + errs) 
  bigmat[ , (i-1)*11+5] = mapply(rep(newcoef[26], n_obs), 
                                  FUN = assignval, 
                                  c2 = rep(newcoef[27], n_obs), 
                                  c3 = rep(newcoef[28], n_obs),
                                  c4 = rep(newcoef[29], n_obs),
                                  val = Xm5%*%as.matrix(newcoef[1:25]) + errs) 
  bigmat[ , (i-1)*11+6] = mapply(rep(newcoef[26], n_obs), 
                                  FUN = assignval, 
                                  c2 = rep(newcoef[27], n_obs), 
                                  c3 = rep(newcoef[28], n_obs),
                                  c4 = rep(newcoef[29], n_obs),
                                  val = X0%*%as.matrix(newcoef[1:25]) + errs)  
  bigmat[ , (i-1)*11+7] = mapply(rep(newcoef[26], n_obs), 
                                  FUN = assignval, 
                                  c2 = rep(newcoef[27], n_obs), 
                                  c3 = rep(newcoef[28], n_obs),
                                  c4 = rep(newcoef[29], n_obs),
                                  val = X5%*%as.matrix(newcoef[1:25]) + errs)  
  bigmat[ , (i-1)*11+8] = mapply(rep(newcoef[26], n_obs), 
                                  FUN = assignval, 
                                  c2 = rep(newcoef[27], n_obs), 
                                  c3 = rep(newcoef[28], n_obs),
                                  c4 = rep(newcoef[29], n_obs),
                                  val = X10%*%as.matrix(newcoef[1:25]) + errs) 
  bigmat[ , (i-1)*11+9] = mapply(rep(newcoef[26], n_obs), 
                                  FUN = assignval, 
                                  c2 = rep(newcoef[27], n_obs), 
                                  c3 = rep(newcoef[28], n_obs),
                                  c4 = rep(newcoef[29], n_obs),
                                  val = X15%*%as.matrix(newcoef[1:25]) + errs) 
  bigmat[ , (i-1)*11+10] = mapply(rep(newcoef[26], n_obs), 
                                   FUN = assignval, 
                                   c2 = rep(newcoef[27], n_obs), 
                                   c3 = rep(newcoef[28], n_obs),
                                   c4 = rep(newcoef[29], n_obs),
                                   val = X20%*%as.matrix(newcoef[1:25]) + errs)  
  bigmat[ , (i-1)*11+11] = mapply(rep(newcoef[26], n_obs), 
                                   FUN = assignval, 
                                   c2 = rep(newcoef[27], n_obs), 
                                   c3 = rep(newcoef[28], n_obs),
                                   c4 = rep(newcoef[29], n_obs),
                                   val = X25%*%as.matrix(newcoef[1:25]) + errs) 
  if (i %% 20 == 0) print(i)
} 


write.table(bigmat, "step1_simNatEcon08_Inc.txt")
rm(bigmat)


######## step2Model2 and Computing the effects #######

df=data
df$IDnum = 1:nrow(df)
grouponly=subset(df, select = c(stateID, IDnum))

## Import the estimated parameters for Model2 produced by step1Model2.R
maincoefs = read.table("mcmc08_step1Model2_MainCoeffs.txt")
groupcoefs = read.table("mcmc08_step1Model2_GroupCoeffs.txt") 
maincoefs = maincoefs[12301:12500,]
groupcoefs = groupcoefs[12301:12500,]
groupcoefsID=NA
for(i in 1:ncol(groupcoefs)){ # This is to adjust the order of the colnames of groupcoefs; the order is NOT 1,2,3,... BUT 1,10,100,101, ...
  ID=str_extract(colnames(groupcoefs)[i], "[0-9]+$") # Extract the digits located at the end of each string 
  groupcoefsID[i]=as.numeric(ID) 
}

## Import the simulated potential mediators produced by step2Model1.R
simMediators=read.table("step1_simNatEcon08_Inc.txt")

# This is a function that converts simulated national econ evaluations to a matrix
convertEval=function(retnum){
  if (is.na(retnum)){out = c(NA, NA, NA, NA, NA)}
  else if (retnum==1){out=as.vector(c(1, 1, 0, 0, 0))}
  else if (retnum==2){out=as.vector(c(1, 0, 1, 0, 0))}
  else if (retnum==3){out=as.vector(c(1, 0, 0, 0, 0))}
  else if (retnum==4){out=as.vector(c(1, 0, 0, 1, 0))}
  else if (retnum==5){out=as.vector(c(1, 0, 0, 0, 1))}
  out
}


################ NOTE ################################################
# Depending on the size of simMediators, it may take long to run below
holder1 = apply(simMediators[1:5000,], FUN = convertEval, MARGIN = c(1, 2))
holder2 = apply(simMediators[5001:10000,], FUN = convertEval, MARGIN = c(1, 2))
holder3 = apply(simMediators[10001:15000,], FUN = convertEval, MARGIN = c(1, 2))
holder4 = apply(simMediators[15001:20000,], FUN = convertEval, MARGIN = c(1, 2))
holder5 = apply(simMediators[20001:nrow(simMediators),], FUN = convertEval, MARGIN = c(1, 2))
holder = abind(holder1, holder2, holder3, holder4, holder5, along = 2)
dim(holder)

source("step2Model2ComputingEff_2008_Inc.R")
avg.eff = step2Model2ComputingEff_2008_Inc(sims=sims)


write.table(avg.eff, "output_inc08")

